# -*- coding: utf-8 -*-

import os

from flask import Flask
from flask_login import LoginManager
from flask_socketio import SocketIO
from flask_cors import CORS

from .models import User

os.environ['PYTHONUNBUFFERED '] = '1'

if not os.path.exists('temp'):
    os.mkdir('temp')
if not os.path.exists('temp/dataset'):
    os.mkdir('temp/dataset')
if not os.path.exists('temp/embed'):
    os.mkdir('temp/embed')
if not os.path.exists('temp/log'):
    os.mkdir('temp/log')
if not os.path.exists('temp/upload'):
    os.mkdir('temp/upload')
if not os.path.exists('temp/forecast_result/'):
    os.mkdir('temp/forecast_result')
if not os.path.exists('temp/forecast_result/AV_Infection'):
    os.mkdir('temp/forecast_result/AV_Infection')

login_manager = LoginManager()
login_manager.login_view = 'auth.login'


@login_manager.user_loader
def load_user(user_id):
    return User.get(user_id)


app = Flask(__name__)
CORS(app, resources=r'/*')

app.secret_key = str("abcdefghijklmnopqrstuvwx")
app.config['JSON_AS_ASCII'] = False
# set app host to 0.0.0.0
app.config['HOST'] = '0.0.0.0'
socket = SocketIO(app, async_mode='threading')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
app.config['UPLOAD_FOLDER'] = 'temp/upload'
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024
login_manager.init_app(app)

# auth module
from .auth import auth as auth_blueprint

app.register_blueprint(auth_blueprint, url_prefix="/auth")

# algorithm route
from .complex import complex as complex_blueprint

app.register_blueprint(complex_blueprint)

# network api sample
from .networkapi import networkapi as networkapi_blueprint

app.register_blueprint(networkapi_blueprint, url_prefix="/networkapi")

from .networkitapi import networkitapi as networkitapi_blueprint

app.register_blueprint(networkitapi_blueprint, url_prefix="/networkitapi")
